package com.ww.microrpc.netty.handler.client;

import com.ww.microrpc.netty.protocol.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

public class PaddingRequestContext {

    private static final Logger logger = LoggerFactory.getLogger(PaddingRequestContext.class);

    private static final ConcurrentHashMap<Integer, RpcFuture> pendingHashMap = new ConcurrentHashMap<>();

    public static void padding(Integer requestId, RpcFuture rpcFuture){
        pendingHashMap.put(requestId, rpcFuture);
    }

    public static void finished(ResponseMessage response){
        Integer requestId = response.getRequestId();
        RpcFuture rpcFuture = pendingHashMap.get(requestId);
        if (rpcFuture != null) {
            rpcFuture.done(response);
            // remove request
            pendingHashMap.remove(requestId);
        } else {
            logger.warn("Can not get pending response for request id: " + requestId);
        }
    }
}
